package com.hongye.house.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hongye.common.constant.ExamineStatusConstant;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.R;
import com.hongye.house.entity.ExamineHomeEntity;
import com.hongye.house.entity.RequirementEntity;
import com.hongye.house.entity.SaleHomeEntity;
import com.hongye.house.fegin.PayFeign;
import com.hongye.house.service.ExamineHomeService;
import com.hongye.house.service.RequirementService;
import com.hongye.house.service.SaleHomeService;
import com.hongye.house.vo.HongyeOrderVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;


/**
 * 审核表
 *
 * @author lins
 * @email 695565697@qq.com
 * @date 2021-10-14 13:32:23
 */
@RestController
@RequestMapping("house/examinehome")
public class ExamineHomeController {
    @Autowired
    private ExamineHomeService examineHomeService;
    @Autowired
    PayFeign payFeign;
    //需求表
    @Autowired
    RequirementService requirementService;
    //房源表
    @Autowired
    SaleHomeService saleHomeService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    /*
    * 求租求购列表加入es
    * */
    @RequestMapping("requirementToEs")
    public R requirementToEs(){
        return examineHomeService.requirementToEs();
    }

    /*
    *  通过审核表id获取商户订单号
    * */
    @RequestMapping("/getOrderSnByExamineId")
    public String getOrderSnByExamineId(@RequestParam Integer examineId){
        return examineHomeService.getOrderSnByExamineId(examineId);
    }
    /**
     * 关闭订单对应审核表
     */
    @RequestMapping("/closeExamine")
    public R closeExamine(@RequestBody HongyeOrderVo hongyeOrderVo){
        examineHomeService.closeExamine(hongyeOrderVo);

        return R.ok();
    }

    /**
     * 订单审核失败接口
     */
    @RequestMapping("auditFailed")
    public R orderReview(@RequestParam("examineId") Integer examineId) {

        ExamineHomeEntity examine_id = examineHomeService.getOne(new QueryWrapper<ExamineHomeEntity>().eq("examine_id", examineId));
        examine_id.setExamineStatus(ExamineStatusConstant.EXAMINE_NO_PASSED);
        boolean b = examineHomeService.updateById(examine_id);
        if (b){
            rabbitTemplate.convertAndSend("rebund-exchange","rebund",examineId);
            return R.ok("审核成功");
        }

        return R.error("审核失败！");
    }
    /**
     * 订单审核通过接口
     * */
    @RequestMapping("Approved")
    public R Approved(@RequestParam("examineId") Integer examineId){
        ExamineHomeEntity examine_id = examineHomeService.getOne(new QueryWrapper<ExamineHomeEntity>().eq("examine_id", examineId));
        examine_id.setExamineStatus(ExamineStatusConstant.EXAMINE_PASSED);
        SaleHomeEntity house_id = saleHomeService.getOne(new QueryWrapper<SaleHomeEntity>().eq("house_id", examine_id.getRequirementId()));
        if (house_id!=null){
            house_id.setHouseDelete(0);
            saleHomeService.updateById(house_id);
        }else{
            RequirementEntity requirement_id = requirementService.getOne(new QueryWrapper<RequirementEntity>().eq("requirement_id", examine_id.getRequirementId()));
            requirement_id.setRequirementStatus(1);
            requirementService.updateById(requirement_id);
        }
        return R.ok("审核通过！");
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = examineHomeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{examineId}")
    public R info(@PathVariable("examineId") Integer examineId){
		ExamineHomeEntity examineHome = examineHomeService.getById(examineId);

        return R.ok().put("examineHome", examineHome);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ExamineHomeEntity examineHome){
		examineHomeService.save(examineHome);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ExamineHomeEntity examineHome){
		examineHomeService.updateById(examineHome);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] examineIds){
		examineHomeService.removeByIds(Arrays.asList(examineIds));

        return R.ok();
    }

}
